cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //    default: null,      // The default value will be used only when the component attaching
        //                           to a node for the first time
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
        a: false,
        d: false,
        w: false,
        s: false,
        speed: 200,
    },

    // use this for initialization
    onLoad: function () {
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onkeyup, this);
    },

    // called every frame, uncomment this function to activate update callback
    update: function (dt) {
        if (this.a) {
            var x = this.node.x - this.speed * dt;
            this.node.x = x;
        }
        if (this.d) {
            var x = this.node.x + this.speed * dt;
            this.node.x = x;
        }
        if (this.w) {
            var y = this.node.y + this.speed * dt;
            this.node.y = y;
        }
        if (this.s) {
            var y = this.node.y - this.speed * dt;
            this.node.y = y;
        }
    },

    onKeyDown: function (event) {
        switch (event.keyCode) {
            case cc.macro.KEY.a:
            case cc.macro.KEY.left:
                this.a = true;
                break;
            case cc.macro.KEY.d:
            case cc.macro.KEY.right:
                this.d = true;
                break;
            case cc.macro.KEY.w:
            case cc.macro.KEY.up:
                this.w = true;
                break;
            case cc.macro.KEY.s:
            case cc.macro.KEY.down:
                this.s = true;
                break;
        }
    },
    onkeyup: function (event) {
        switch (event.keyCode) {
            case cc.macro.KEY.a:
            case cc.macro.KEY.left:
                this.a = false;
                break;
            case cc.macro.KEY.d:
            case cc.macro.KEY.right:
                this.d = false;
                break;
            case cc.macro.KEY.w:
            case cc.macro.KEY.up:
                this.w = false;
                break;
            case cc.macro.KEY.s:
            case cc.macro.KEY.down:
                this.s = false;
                break;
        }
    }
});
